import socket

from src.Front.service.socket_abc import BaseSocket


class SocketServer(BaseSocket):

    def __init__(self, ip_add='localhost', port=33333):
        self.ip_add = ip_add
        self.port = port
        self.client_list = []
        self.socket = None
        self.has_open = False

    def start(self):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.bind((self.ip_add, self.port))
        self.socket.listen()
        self.has_open = True
        print('[info] socket server start')

    def send_msg(self, msg):
        print('[info] ready to send msg')
        temp_msg = bytes(msg, encoding='utf-8')
        for client in self.client_list:
            client.sendall(temp_msg)
        print('[info] send msg success')


if __name__ == '__main__':
    s = SocketServer()
    s.start()
